package com.tju.ocean_ecology_website.service.impl;

import com.tju.ocean_ecology_website.entity.VolunteerComment;
import com.tju.ocean_ecology_website.entity.VolunteerCommentLike;
import com.tju.ocean_ecology_website.dao.VolunteerCommentDao;
import com.tju.ocean_ecology_website.dao.VolunteerCommentLikeDao;
import com.tju.ocean_ecology_website.service.VolunteerCommentService;
import org.springframework.stereotype.Service;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * 志愿者评论表(VolunteerComment)表服务实现类
 *
 * @author shaolong
 * @since 2025-05-05 10:00:00
 */
@Service("volunteerCommentService")
public class VolunteerCommentServiceImpl implements VolunteerCommentService {
    @Resource
    private VolunteerCommentDao volunteerCommentDao;
    
    @Resource
    private VolunteerCommentLikeDao volunteerCommentLikeDao;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public VolunteerComment queryById(Long id) {
        return this.volunteerCommentDao.queryById(id);
    }

    /**
     * 分页查询
     *
     * @param volunteerComment 筛选条件
     * @param pageRequest      分页对象
     * @return 查询结果
     */
    @Override
    public Page<VolunteerComment> queryByPage(VolunteerComment volunteerComment, PageRequest pageRequest) {
        long total = this.volunteerCommentDao.count(volunteerComment);
        return new PageImpl<>(this.volunteerCommentDao.queryAllByLimit(volunteerComment, pageRequest), pageRequest, total);
    }

    /**
     * 新增数据
     *
     * @param volunteerComment 实例对象
     * @return 实例对象
     */
    @Override
    public VolunteerComment insert(VolunteerComment volunteerComment) {
        // 设置创建时间和更新时间
        Date now = new Date();
        volunteerComment.setCreateTime(now);
        volunteerComment.setUpdateTime(now);
        
        // 设置点赞数和状态
        volunteerComment.setLikeCount(0);
        volunteerComment.setStatus(0); // 正常
        
        this.volunteerCommentDao.insert(volunteerComment);
        return volunteerComment;
    }

    /**
     * 修改数据
     *
     * @param volunteerComment 实例对象
     * @return 实例对象
     */
    @Override
    public VolunteerComment update(VolunteerComment volunteerComment) {
        // 设置更新时间
        volunteerComment.setUpdateTime(new Date());
        
        this.volunteerCommentDao.update(volunteerComment);
        return this.queryById(volunteerComment.getId());
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Long id) {
        return this.volunteerCommentDao.deleteById(id) > 0;
    }

    /**
     * 根据用户ID查询评论
     *
     * @param userId 用户ID
     * @param pageRequest 分页对象
     * @return 评论列表
     */
    @Override
    public Page<VolunteerComment> queryByUserId(Long userId, PageRequest pageRequest) {
        long total = this.volunteerCommentDao.countByUserId(userId);
        List<VolunteerComment> comments = this.volunteerCommentDao.queryByUserId(userId, pageRequest);
        return new PageImpl<>(comments, pageRequest, total);
    }

    /**
     * 根据目标类型和ID查询评论
     *
     * @param targetType 目标类型
     * @param targetId 目标ID
     * @param pageRequest 分页对象
     * @return 评论列表
     */
    @Override
    public Page<VolunteerComment> queryByTarget(Integer targetType, Long targetId, PageRequest pageRequest) {
        long total = this.volunteerCommentDao.countByTarget(targetType, targetId);
        List<VolunteerComment> comments = this.volunteerCommentDao.queryByTarget(targetType, targetId, pageRequest);
        return new PageImpl<>(comments, pageRequest, total);
    }

    /**
     * 点赞/取消点赞评论
     *
     * @param commentId 评论ID
     * @param userId 用户ID
     * @return 是否成功
     */
    @Override
    @Transactional
    public boolean toggleLike(Long commentId, Long userId) {
        // 查询是否已点赞
        VolunteerCommentLike like = this.volunteerCommentLikeDao.queryByUserAndComment(userId, commentId);
        
        if (like == null) {
            // 未点赞，添加点赞记录
            like = new VolunteerCommentLike();
            like.setUserId(userId);
            like.setCommentId(commentId);
            like.setCreateTime(new Date());
            this.volunteerCommentLikeDao.insert(like);
            
            // 增加评论点赞数
            this.volunteerCommentDao.increaseLikeCount(commentId);
            return true;
        } else {
            // 已点赞，取消点赞
            this.volunteerCommentLikeDao.deleteByUserAndComment(userId, commentId);
            
            // 减少评论点赞数
            this.volunteerCommentDao.decreaseLikeCount(commentId);
            return false;
        }
    }

    /**
     * 检查用户是否点赞评论
     *
     * @param commentId 评论ID
     * @param userId 用户ID
     * @return 是否点赞
     */
    @Override
    public boolean isLiked(Long commentId, Long userId) {
        VolunteerCommentLike like = this.volunteerCommentLikeDao.queryByUserAndComment(userId, commentId);
        return like != null;
    }

    /**
     * 获取评论的点赞用户列表
     *
     * @param commentId 评论ID
     * @return 点赞用户ID列表
     */
    @Override
    public List<Long> getLikeUsers(Long commentId) {
        return this.volunteerCommentLikeDao.queryLikeUsersByComment(commentId);
    }
}
